package com.jack.payment.util.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.jack.payment.util.common.StringUtil;

import java.io.IOException;

public class SensitiveSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (!StringUtil.isEmpty(s)) {
            if(StringUtil.isEmail(s)) {
                String[] strSplitArray = s.split("@");
                s = strSplitArray[0].replaceAll("(?<=^.{3}).*(?=.{3}$)", "****")
                        + "@" +  strSplitArray[strSplitArray.length - 1];
            } else {
                s = s.replaceAll("(?<=^.{3}).*(?=.{4}$)", "****");
            }
        }
        jsonGenerator.writeObject(s);
    }
}
